#include "hplccommonsystempage.h"
#include "modulemanager.h"
#include "modulehplcpump.h"
#include "modulehplcpumpparamiddefine.h"
#include "modulehplccmdid.h"
#include <QCloseEvent>

HplcCommonSystemPage::HplcCommonSystemPage(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Hplc System Common Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    ModuleHplcPump *mod = static_cast<ModuleHplcPump*>(moduleManager->find(ModuleID_HPLC_PumpA));
    if (mod == nullptr)
        return;
    connect(mod, &ModuleHplcPump::parseCompleted,
            this, &HplcCommonSystemPage::onParseCompleted);


    // 复位
    m_lbReset = new QLabel(tr("Reset"));
    m_leReset = new QLineEdit();
    m_pbSetReset = new QPushButton(tr("Set"));
    connect(m_pbSetReset, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 address = m_leReset->text().toUInt();
        var.setValue(address);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetReset, var);
    });
    m_pbGetReset = new QPushButton(tr("Get"));
    m_pbGetReset->setEnabled(false);
    hblReset = new QHBoxLayout();
    hblReset->addWidget(m_lbReset);
    hblReset->addWidget(m_leReset);
    hblReset->addWidget(m_pbSetReset);
    hblReset->addWidget(m_pbGetReset);

    // 恢复出厂设置
    m_lbRestoreDefault = new QLabel(tr("RestoreDefault"));
    m_leRestoreDefault = new QLineEdit();
    m_pbSetRestoreDefault = new QPushButton(tr("Set"));
    connect(m_pbSetRestoreDefault, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 address = m_leRestoreDefault->text().toUInt();
        var.setValue(address);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetRestoreDefault, var);
    });
    m_pbGetRestoreDefault = new QPushButton(tr("Get"));
    m_pbGetRestoreDefault->setEnabled(false);
    hblRestoreDefault = new QHBoxLayout();
    hblRestoreDefault->addWidget(m_lbRestoreDefault);
    hblRestoreDefault->addWidget(m_leRestoreDefault);
    hblRestoreDefault->addWidget(m_pbSetRestoreDefault);
    hblRestoreDefault->addWidget(m_pbGetRestoreDefault);

    // 保存为出厂设置
    m_lbSaveAsDefault = new QLabel(tr("SaveAsDefault"));
    m_leSaveAsDefault = new QLineEdit();
    m_leSaveAsDefault->setEnabled(false);
    m_pbSetSaveAsDefault = new QPushButton(tr("Set"));
    connect(m_pbSetSaveAsDefault, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 address = m_leSaveAsDefault->text().toUInt();
        var.setValue(address);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetSaveAsDefault, var);
    });
    m_pbGetSaveAsDefault = new QPushButton(tr("Get"));
    m_pbGetSaveAsDefault->setEnabled(false);
    hblSaveAsDefault = new QHBoxLayout();
    hblSaveAsDefault->addWidget(m_lbSaveAsDefault);
    hblSaveAsDefault->addWidget(m_leSaveAsDefault);
    hblSaveAsDefault->addWidget(m_pbSetSaveAsDefault);
    hblSaveAsDefault->addWidget(m_pbGetSaveAsDefault);

    // 超级密码
    m_lbSuperPassword = new QLabel(tr("SuperPassword"));
    m_leSuperPassword = new QLineEdit();
    m_pbSetSuperPassword = new QPushButton(tr("Set"));
    connect(m_pbSetSuperPassword, &QPushButton::clicked, this, [=](){
        QVariant var;
        QString str = m_leSuperPassword->text();
        var.setValue(str);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetSuperPassword, var);
    });
    m_pbGetSuperPassword = new QPushButton(tr("Get"));
    connect(m_pbGetSuperPassword, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetSuperPassword, var);
    });
    hblSuperPassword = new QHBoxLayout();
    hblSuperPassword->addWidget(m_lbSuperPassword);
    hblSuperPassword->addWidget(m_leSuperPassword);
    hblSuperPassword->addWidget(m_pbSetSuperPassword);
    hblSuperPassword->addWidget(m_pbGetSuperPassword);

    // 软件版本
    m_lbSoftwareVersion = new QLabel(tr("SoftwareVersion"));
    m_leSoftwareVersion = new QLineEdit();
    m_pbSetSoftwareVersion = new QPushButton(tr("Set"));
    m_pbSetSoftwareVersion->setEnabled(false);
    m_pbGetSoftwareVersion = new QPushButton(tr("Get"));
    connect(m_pbGetSoftwareVersion, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetSoftwareVersion, var);
    });
    hblSoftwareVersion = new QHBoxLayout();
    hblSoftwareVersion->addWidget(m_lbSoftwareVersion);
    hblSoftwareVersion->addWidget(m_leSoftwareVersion);
    hblSoftwareVersion->addWidget(m_pbSetSoftwareVersion);
    hblSoftwareVersion->addWidget(m_pbGetSoftwareVersion);

    // 硬件版本
    m_lbHardwareVersion = new QLabel(tr("HardwareVersion"));
    m_leHardwareVersion = new QLineEdit();
    m_pbSetHardwareVersion = new QPushButton(tr("Set"));
    m_pbSetHardwareVersion->setEnabled(false);
    m_pbGetHardwareVersion = new QPushButton(tr("Get"));
    connect(m_pbGetHardwareVersion, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetHardwareVersion, var);
    });
    hblHardwareVersion = new QHBoxLayout();
    hblHardwareVersion->addWidget(m_lbHardwareVersion);
    hblHardwareVersion->addWidget(m_leHardwareVersion);
    hblHardwareVersion->addWidget(m_pbSetHardwareVersion);
    hblHardwareVersion->addWidget(m_pbGetHardwareVersion);

    // 生产日期
    m_lbProductDate = new QLabel(tr("ProductDate"));
    m_leProductDate = new QLineEdit();
    m_pbSetProductDate = new QPushButton(tr("Set"));
    connect(m_pbSetProductDate, &QPushButton::clicked, this, [=](){
        QVariant var;
        QString str = m_leProductDate->text();
        var.setValue(str);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetProductDate, var);
    });
    m_pbGetProductDate = new QPushButton(tr("Get"));
    connect(m_pbGetProductDate, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetProductDate, var);
    });
    hblProductDate = new QHBoxLayout();
    hblProductDate->addWidget(m_lbProductDate);
    hblProductDate->addWidget(m_leProductDate);
    hblProductDate->addWidget(m_pbSetProductDate);
    hblProductDate->addWidget(m_pbGetProductDate);

    // 序列号
    m_lbSerialNumber = new QLabel(tr("SerialNumber"));
    m_leSerialNumber = new QLineEdit();
    m_pbSetSerialNumber = new QPushButton(tr("Set"));
    connect(m_pbSetSerialNumber, &QPushButton::clicked, this, [=](){
        QVariant var;
        QString str = m_leSerialNumber->text();
        var.setValue(str);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetSerialNumber, var);
    });
    m_pbGetSerialNumber = new QPushButton(tr("Get"));
    connect(m_pbGetSerialNumber, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetSerialNumber, var);
    });
    hblSerialNumber = new QHBoxLayout();
    hblSerialNumber->addWidget(m_lbSerialNumber);
    hblSerialNumber->addWidget(m_leSerialNumber);
    hblSerialNumber->addWidget(m_pbSetSerialNumber);
    hblSerialNumber->addWidget(m_pbGetSerialNumber);

    // 型号
    m_lbModel = new QLabel(tr("Model"));
    m_leModel = new QLineEdit();
    m_pbSetModel = new QPushButton(tr("Set"));
    connect(m_pbSetModel, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 tmp = m_leModel->text().toUInt();
        var.setValue(tmp);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetModel, var);
    });
    m_pbGetModel = new QPushButton(tr("Get"));
    connect(m_pbGetModel, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetModel, var);
    });
    hblModel = new QHBoxLayout();
    hblModel->addWidget(m_lbModel);
    hblModel->addWidget(m_leModel);
    hblModel->addWidget(m_pbSetModel);
    hblModel->addWidget(m_pbGetModel);

    // 型号字符串
    m_lbModelString = new QLabel(tr("ModelString"));
    m_leModelString = new QLineEdit();
    m_pbSetModelString = new QPushButton(tr("Set"));
    connect(m_pbSetModelString, &QPushButton::clicked, this, [=](){
        QVariant var;
        QString str = m_leModelString->text();
        var.setValue(str);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetModelString, var);
    });
    m_pbGetModelString = new QPushButton(tr("Get"));
    connect(m_pbGetModelString, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetModelString, var);
    });
    hblModelString = new QHBoxLayout();
    hblModelString->addWidget(m_lbModelString);
    hblModelString->addWidget(m_leModelString);
    hblModelString->addWidget(m_pbSetModelString);
    hblModelString->addWidget(m_pbGetModelString);

    // 使用时间
    m_lbUsedTime = new QLabel(tr("UsedTime"));
    m_leUsedTime = new QLineEdit();
    m_pbSetUsedTime = new QPushButton(tr("Set"));
    connect(m_pbSetUsedTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 tmp = m_leUsedTime->text().toUInt();
        var.setValue(tmp);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetUsedTime, var);
    });
    m_pbGetUsedTime = new QPushButton(tr("Get"));
    connect(m_pbGetUsedTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetUsedTime, var);
    });
    hblUsedTime = new QHBoxLayout();
    hblUsedTime->addWidget(m_lbUsedTime);
    hblUsedTime->addWidget(m_leUsedTime);
    hblUsedTime->addWidget(m_pbSetUsedTime);
    hblUsedTime->addWidget(m_pbGetUsedTime);

    // 全部数据
    m_lbAllData = new QLabel(tr("AllData"));
    m_leAllData = new QLineEdit();
    m_pbSetAllData = new QPushButton(tr("Set"));
    m_pbSetAllData->setEnabled(false);
    m_pbGetAllData = new QPushButton(tr("Get"));
    connect(m_pbGetAllData, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetAllData, var);
    });
    hblAllData = new QHBoxLayout();
    hblAllData->addWidget(m_lbAllData);
    hblAllData->addWidget(m_leAllData);
    hblAllData->addWidget(m_pbSetAllData);
    hblAllData->addWidget(m_pbGetAllData);

    // 协议
    m_lbProtocol = new QLabel(tr("Protocol"));
    m_leProtocol = new QLineEdit();
    m_pbSetProtocol = new QPushButton(tr("Set"));
    connect(m_pbSetProtocol, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 tmp = m_leProtocol->text().toUInt();
        var.setValue(tmp);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetProtocol, var);
    });
    m_pbGetProtocol = new QPushButton(tr("Get"));
    connect(m_pbGetProtocol, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetProtocol, var);
    });
    hblProtocol = new QHBoxLayout();
    hblProtocol->addWidget(m_lbProtocol);
    hblProtocol->addWidget(m_leProtocol);
    hblProtocol->addWidget(m_pbSetProtocol);
    hblProtocol->addWidget(m_pbGetProtocol);



    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblReset);
    vbl->addLayout(hblRestoreDefault);
    vbl->addLayout(hblSaveAsDefault);
    vbl->addLayout(hblSuperPassword);
    vbl->addLayout(hblSoftwareVersion);
    vbl->addLayout(hblHardwareVersion);
    vbl->addLayout(hblProductDate);
    vbl->addLayout(hblSerialNumber);
    vbl->addLayout(hblModel);
    vbl->addLayout(hblModelString);
    vbl->addLayout(hblUsedTime);
    vbl->addLayout(hblAllData);
    vbl->addLayout(hblProtocol);
    setLayout(vbl);
}

HplcCommonSystemPage::~HplcCommonSystemPage()
{

}

void HplcCommonSystemPage::closeEvent(QCloseEvent *event)
{
    disconnect();
    event->accept();
}

void HplcCommonSystemPage::onParseCompleted(int id, const QVariant &var)
{
    switch (id)
    {
        case hplc::CommonSystemCmdID_SetReset:
        {
            m_leReset->setText("1");
        }
        break;
        case hplc::CommonSystemCmdID_SetSuperPassword:
        {
            QString str = var.toString();
            m_leSuperPassword->setText(str);
        }
        break;
        case hplc::CommonSystemCmdID_SetSoftwareVersion:
        {
            QString str = var.toString();
            m_leSoftwareVersion->setText(str);
        }
        break;
        case hplc::CommonSystemCmdID_SetHardwareVersion:
        {
            QString str = var.toString();
            m_leHardwareVersion->setText(str);
        }
        break;
        case hplc::CommonSystemCmdID_SetProductDate:
        {
            QString str = var.toString();
            m_leProductDate->setText(str);
        }
        break;
        case hplc::CommonSystemCmdID_SetSerialNumber:
        {
            QString str = var.toString();
            m_leSerialNumber->setText(str);
        }
        break;
        case hplc::CommonSystemCmdID_SetModel:
        {
            quint8 model = var.toUInt();
            m_leModel->setText(QString::number(model));
        }
        break;
        case hplc::CommonSystemCmdID_SetModelString:
        {
            QString str = var.toString();
            m_leModelString->setText(str);
        }
        break;
        case hplc::CommonSystemCmdID_SetUsedTime:
        {
            quint32 time = var.toUInt();
            m_leUsedTime->setText(QString::number(time));
        }
        break;
        case hplc::CommonSystemCmdID_SetProtocol:
        {
            quint8 protocol = var.toUInt();
            m_leProtocol->setText(QString::number(protocol));
        }
        break;
        default:
        break;
    }
}
